package com.example.demo.controller;

import com.example.demo.VO.Result;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2020-12-08 14:12:25
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }

    @PostMapping("login")
    public Result login(String stuid, String password){
        if(StringUtils.isNotBlank(stuid) || StringUtils.isNotBlank(password)){
            return Result.failure("请输入账号密码");
        }
        User user = new User();
        user.setUserStuid(stuid);
        user.setUserPassword(password);
        List<User> list = userService.queryByExample(user);
        if (list.size()==1){
            return Result.success(list.get(0).getToken());
        }else {
            return Result.failure("账号或密码错误");
        }
    }

    @PostMapping("register")
    public Result register(User user){
        User user1 = new User();
        user1.setUserStuid(user.getUserStuid());
        if(userService.queryByExample(user1).size()==1){
            return Result.failure("学号重复");
        }
        userService.insert(user);
        return Result.success();
    }

    @DeleteMapping("delete")
    public Result delete(Integer stuid){
        return Result.success(userService.deleteById(stuid));
    }
}
















